/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.edu.thptduongbachmai.model.data;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import vn.edu.thptduongbachmai.DbUtil.Criteria;
import vn.edu.thptduongbachmai.DbUtil.DatabaseUtil;
import vn.edu.thptduongbachmai.DbUtil.Order;
import vn.edu.thptduongbachmai.DbUtil.Projections;
import vn.edu.thptduongbachmai.DbUtil.Restrictions;

/**
 *
 * @author Son
 */
public abstract class AbstractModel<P, K extends Serializable> {

    protected abstract Class getPOJOClass();
    Logger _logger = Logger.getLogger(getPOJOClass());

    protected List<P> getAll() {
        Criteria criteria = new Criteria();
        return DatabaseUtil.Instance.getWithCriteria(getPOJOClass(), criteria);
    }

    protected P get(K key) {
        P p = (P) DatabaseUtil.Instance.get(getPOJOClass(), key);
        return p;
    }

    protected List<P> getSlideListDateModifyDesc(long begin, int num) {
        Criteria criteria = new Criteria();
        criteria.setFirstResult(begin);
        criteria.setMaxResults(num);
        criteria.addOther(Order.desc("ngayCapNhat"));
        return DatabaseUtil.Instance.getWithCriteria(getPOJOClass(), criteria);
    }

    public List<P> getWithParameters(Map<String, Object> mapProperty,
            Map<String, Object> orderBy, Map<String, Object> mapLike,
            String property,int begin, int num) {
        Criteria criteria = new Criteria();
        if (mapProperty != null) {
            for (Map.Entry<String, Object> entry : mapProperty.entrySet()) {
                criteria.add(Restrictions.eq(entry.getKey(), entry.getValue()));

            }
        }

        if (mapLike != null) {
            for (Map.Entry<String, Object> entry : mapLike.entrySet()) {
                criteria.add(Restrictions.like(entry.getKey(), entry.getValue()));

            }
        }

        
        if (orderBy != null) {
            if (orderBy.get("type").equals("asc")) {
                criteria.addOther(Order.asc(String.valueOf(orderBy.get("property"))));
            } else {
                criteria.addOther(Order.desc(String.valueOf(orderBy.get("property"))));
            }

        }
        if (begin != -1) {
            criteria.setFirstResult(begin);
            criteria.setMaxResults(num);
        }
        List<P> list = DatabaseUtil.Instance.getWithCriteria(getPOJOClass(), criteria);
        return list;
    }
    protected boolean save(P p) {
        return DatabaseUtil.Instance.executeUpdate(getPOJOClass(), p);
    }

    protected int getCount() {
        Criteria criteria = new Criteria();
        criteria.setProjection(Projections.rowCount());
        List<Long> listResult = DatabaseUtil.Instance.getWithCriteria(getPOJOClass(), criteria);
        if (listResult != null && !listResult.isEmpty()) {

            return listResult.get(0).intValue();
        }
        return 0;
    }

    protected int getCount(Criteria criteria) {
        criteria.setProjection(Projections.rowCount());
        List<Long> listResult = DatabaseUtil.Instance.getWithCriteria(getPOJOClass(), criteria);
        if (listResult != null && !listResult.isEmpty()) {

            return listResult.get(0).intValue();
        }
        return 0;
    }
}
